package com.example.tests;

import static com.example.tests.GroupDataGenerator.loadsGroupsFromXmlFile;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Random;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.example.utils.SortedListOf;

public class GroupModificationTest extends TestBase {

	@DataProvider
	public static Iterator<Object[]> groupsFromFile() throws IOException {
		return wrapGroupDataForDataProvider(loadsGroupsFromXmlFile(new File("groups.xml"))).iterator();
	}
	
	@Test(dataProvider = "groupsFromFile")
	public void modifySomeGroup(GroupData group) {

		// save old state
		SortedListOf<GroupData> oldList = app.getGroupHelper().getGroups();
		
		
		Random rnd = new Random();
		int index = rnd.nextInt(oldList.size()-1);

		// action
		app.getGroupHelper().modifyGroup(index, group);


		// save new state
		SortedListOf<GroupData> newList = app.getGroupHelper().getGroups();

		// compare states
		assertThat(newList, equalTo(oldList.without(index).withAdded(group)));
	}

}
